package cn.chenhongchao.zhaobiaopingtai.service.impl;

import cn.chenhongchao.zhaobiaopingtai.common.api.CommonPage;
import cn.chenhongchao.zhaobiaopingtai.constant.BidConstant;
import cn.chenhongchao.zhaobiaopingtai.dto.*;
import cn.chenhongchao.zhaobiaopingtai.entity.*;
import cn.chenhongchao.zhaobiaopingtai.mapper.CategoryMapper;
import cn.chenhongchao.zhaobiaopingtai.mapper.TenderMapper;
import cn.chenhongchao.zhaobiaopingtai.mapper.UsersMapper;
import cn.chenhongchao.zhaobiaopingtai.service.ITenderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TenderServiceImpl extends ServiceImpl<TenderMapper, Tender> implements ITenderService {

    @Resource
    private TenderMapper tenderMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Autowired
    private BidServiceImpl bidService;

    @Resource
    private UsersMapper usersMapper;


    @Override
    public List<TenderWinDTO> getList2(Integer pageNum, Integer pageSize) {
        //分页器
        PageHelper.startPage(pageNum, pageSize);

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(2);
        List<Tender> tenderList =  tenderMapper.selectByExample(example);
        List<TenderWinDTO> tenderWinDTOS = new ArrayList<>();

        for (Tender tender:tenderList
        ) {
            TenderWinDTO t = new TenderWinDTO();
            t.setTender(tender);
            t.setSUPPLIER_NAME(usersMapper.selectByPrimaryKey(tender.getUserid()).getUsername());
            tenderWinDTOS.add(t);
        }
        return tenderWinDTOS;

    }

    @Override
    public List<Tender> getList(Integer pageNum, Integer pageSize) {
        //分页器
        PageHelper.startPage(pageNum, pageSize);

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(2);
        return tenderMapper.selectByExample(example);
    }

    @Override
    public List<Tender> getListzhaobiao(Integer pageNum, Integer pageSize) {
        //分页器
        PageHelper.startPage(pageNum, pageSize);

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();
        criteria.andStateBetween(0,1);
        return tenderMapper.selectByExample(example);
    }



    @Override
    public List<TenderDTO> getListzhaobiao2(Integer pageNum, Integer pageSize,Integer categorynameIndex
            ,String tenderName) {
        //分页器
        if (!(pageNum == null || pageSize == null)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();
        criteria.andStateBetween(0,1);
        if(categorynameIndex != 0 && categorynameIndex != null){
            criteria.andCategoryIdEqualTo(categorynameIndex.longValue());
        }
        if(tenderName != "" && tenderName != null){
            criteria.andTendernameLike("%"+tenderName+"%");
        }
        List<Tender> tenderList =  tenderMapper.selectByExample(example);
        List<TenderDTO> tenderDTOS = new ArrayList<>();

        for (Tender tender:tenderList
        ) {
            TenderDTO t = new TenderDTO();

            t.setTender(tender);
            t.setCategoryName(categoryMapper.selectByPrimaryKey(tender.getCategoryId()).getCategoryname());
            tenderDTOS.add(t);
        }
        return tenderDTOS;
    }

    @Override
    public List<TenderDTO> getListzhaobiao3(Integer pageNum, Integer pageSize,String tenderNumber
            ,String tenderName) {
        //分页器
        if (!(pageNum == null || pageSize == null)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();

        if(tenderNumber != "" && tenderNumber != null){
            criteria.andTenderidEqualTo(Integer.parseInt(tenderNumber));
        }
        if(tenderName != "" && tenderName != null){
            criteria.andTendernameLike("%"+tenderName+"%");
        }
        List<Tender> tenderList =  tenderMapper.selectByExample(example);
        List<TenderDTO> tenderDTOS = new ArrayList<>();

        for (Tender tender:tenderList
        ) {
            TenderDTO t = new TenderDTO();

            t.setTender(tender);
            t.setCategoryName(categoryMapper.selectByPrimaryKey(tender.getCategoryId()).getCategoryname());
            tenderDTOS.add(t);
        }
        return tenderDTOS;
    }


    @Override
    public TenderAndUserDTO getTenderByCode(Integer code) {
        TenderAndUserDTO tenderAndUserDTO = new TenderAndUserDTO();
        Tender tender = tenderMapper.selectByPrimaryKey(code);
        tenderAndUserDTO.setTender(tender);
        Users users = usersMapper.selectByPrimaryKey(tender.getUserid());
        users.setPasswd("");
        tenderAndUserDTO.setUsers(users);
        return tenderAndUserDTO;
    }

    @Override
    public List<TenderDTO> getTenderEvaluation(Integer pageNum, Integer pageSize) {
        //分页器
        if (!(pageNum == null || pageSize == null)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        TenderExample example = new TenderExample();
        TenderExample.Criteria criteria = example.createCriteria();
            criteria.andStateEqualTo(3);

        List<Tender> tenderList =  tenderMapper.selectByExample(example);
        List<TenderDTO> tenderDTOS = new ArrayList<>();

        for (Tender tender:tenderList
        ) {
            TenderDTO t = new TenderDTO();

            t.setTender(tender);
            t.setCategoryName(categoryMapper.selectByPrimaryKey(tender.getCategoryId()).getCategoryname());
            tenderDTOS.add(t);
        }
        return tenderDTOS;
    }

    @Override
    public CommonPage<OpenBidDTO> getTenderList(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {
        QueryWrapper<Tender> wrapper = new QueryWrapper<>();

        // TODO tenderType 不知道对应哪个字段
//        if (!StringUtils.isEmpty(returnBidDTO.getTenderType())) {
//            wrapper.eq("commCode", returnBidDTO.getTenderType());
//        }1

        if (!StringUtils.isEmpty(returnBidDTO.getTenderNumber())) {
            wrapper.eq("tenderId", returnBidDTO.getTenderNumber());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getTenderName())) {
            wrapper.like("tenderName", returnBidDTO.getTenderName());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getCommCode())) {
            wrapper.like("commCode", returnBidDTO.getCommCode());
        }

        // 分页查询
        Page<Tender> page = new Page<>(pageNum, pageSize);
        tenderMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<OpenBidDTO> returnBidCommonPage = new CommonPage<OpenBidDTO>();

        List<Tender> records = page.getRecords();
        List<OpenBidDTO> collect = records.stream().filter(item -> {
            return item.getTenderType().equals("未开标");
        }).map(bid -> {
            OpenBidDTO openBidDTO = new OpenBidDTO();
            openBidDTO.setTenderName(bid.getTendername());
            openBidDTO.setId(bid.getTenderid());
            openBidDTO.setTenderNumber(bid.getTenderid());
            openBidDTO.setTenderFileStatus("!lost");

            // TODO 先设置初始值
            openBidDTO.setCommName("a");
            openBidDTO.setContractDemand("b");

            if (bid.getState() == 1 || bid.getState() == 2) {
                // 开标中 或者 公示
                // System.out.println("bidId=========》" + bid.getTenderid() + "bid.getTenderType():" + bid.getTenderType());
                if (bid.getTenderType().equals("商务标")) {
                    openBidDTO.setTenderScheduleStatus(BidConstant.开商务标);
                } else if (bid.getTenderType().equals("技术标")) {
                    openBidDTO.setTenderScheduleStatus(BidConstant.开技术标);
                } else {
                    openBidDTO.setTenderScheduleStatus(BidConstant.流标);
                }
            } else if (bid.getState() == 0) {
                // 未开标
                openBidDTO.setTenderScheduleStatus(BidConstant.未开标);
            }
            // openBidDTO.setTenderScheduleStatus();
            return openBidDTO;
        }).collect(Collectors.toList());
        returnBidCommonPage.setList(collect);


        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);

        returnBidCommonPage.setPageSize(pageSize);


        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());

        return returnBidCommonPage;
    }

    @Override
    public void getScheduleMax(Integer code, Integer openTenderType) {

//        if(  openTenderType==0){
//            // 修改bid表修改状态码
//            // TODO wrapper 还需要 增加一列过滤 wrapper.eq("userId", "userId");
//            Bid bid = bidService.getOne(new QueryWrapper<Bid>().eq("tenderId", code));
//            bid.setState(4);
//            bid.setStatechina("回标");
//            bidService.updateById(bid);
//        }
//        if (openTenderType==1){
//            Tender tender = tenderMapper.selectByPrimaryKey(code);
//            tender.setState(1);
//            tender.setStatechina("开标中");
//            tenderMapper.update(tender,new QueryWrapper<Tender>().eq("tenderId", code));
//        }
        //中标审定
        Bid bid = bidService.getOne(new QueryWrapper<Bid>().eq("bidId", code));
        bid.setTenderScheduleStatus("tenderWinApprovePASS");
        bidService.updateById(bid);



    }
    @Override
    public void getScheduleMax2(Integer code, Integer openTenderType) {

        if (openTenderType==1){
            Tender tender = tenderMapper.selectByPrimaryKey(code);
            tender.setState(1);
            tender.setStatechina("开标中");
            tenderMapper.update(tender,new QueryWrapper<Tender>().eq("tenderId", code));
        }


    }

    @Override
    public CommonPage<BusinessBid> getOpenBusinessList(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {
        return null;
    }


}
